import requests
import torch
import torch_npu
from torch_npu.contrib import transfer_to_npu
from PIL import Image
from io import BytesIO
from diffusers import StableDiffusionImg2ImgPipeline

torch.npu.set_compile_mode(jit_compile=False)


device = "cuda"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("nitrosocke/Ghibli-Diffusion").to(
    device
)

url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"

response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image.thumbnail((768, 768))

prompt = "ghibli style, a fantasy landscape with castles"
generator = torch.Generator(device="cpu").manual_seed(1024)
image = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5, generator=generator).images[0]
image.save("./image.png")